﻿define(['domReady', 'silder_code', 'validate', 'validate_config', 'jquery-confirm'], function (domReady, silder_code, validate, validate_config, jqueryconfirm) {
 
    var _old_onload = window.onload;
    var $TN = silder_code.tncode;
    var v = $("#loginForm").validate({
        rules: {
            username: {
                required: true
            },
            password: {
                required: true
            }
        },
        messages: {
            username: {
                required: "用户名不能为空"
            },
            password: {
                required: "密码不能为空"
            }
        }
    });
    window.onload = function(){
        if(typeof _old_onload == 'function'){
            _old_onload();
        }
        silder_code.tncode.init();
        //设置表单验证
        silder_code.tncode.onvalidate(function () {
            return v.form();
        })
    };



    $TN.onsuccess(function () {
        //验证通过
        var username = $("#username").val();
        var password = $("#password").val();
        $.ajax({
            url: "/BMS/Login/Login",
            type: "post",
            async: true,
            data: {
                account: username,
                password: password
            },
            dataType: "json",
            success: function (data,textStatus) {
                if (data.code == "200") {
                    var url = getUrlParam('from')
                    if (url != null) {
                        window.location = url;
                    } else {
                        window.location = "/BMS";
                    }
                    
                } else {
                    $.alert({
                        title: '错误提示',
                        icon: 'mdi mdi-alert',
                        type: 'orange',
                        content: data.message,
                    });
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.alert({
                    title: '网络请求错误，请重试。',
                    icon: 'mdi mdi-alert',
                    type: 'orange',
                    content: "",
                });
            }
        })
    });
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return unescape(r[2]); return null; //返回参数值
    }
})